package com.buba.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.pojo.record.Record;
import com.buba.pojo.record.WaitingRecord;
import com.buba.pojo.user.PatientCard;
import com.buba.server.record.ServerRecordService;
import com.buba.user.pojoVo.PayVo;
import com.buba.user.service.PatientCardService;
import com.buba.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 就诊卡表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-06-09
 */
@Slf4j
@RestController
@RequestMapping("/user/patient-card")
public class PatientCardController {
    @Autowired
    private ServerRecordService srService;
    @Autowired
    private PatientCardService pcService;


    //扣款缴费
    @GetMapping("/payment/{cardNo}/{price}")
    public R payment(@PathVariable String cardNo , @PathVariable String price){
        Boolean flag = pcService.payment(cardNo , price);
        if (flag){
            return R.ok().data("message" , "缴费成功");
        }else{
            return R.error().data("message" , "缴费失败");
        }
    }

    /**
     * todo 根据userId获取余额
     *
     * @param userId
     * @return com.buba.utils.R
     * @author xhh
     * @data 2022/6/10 9:10
     */
    @RequestMapping(value = "/getBlaceByUserId",method = RequestMethod.POST)
    public R getBlaceByUserId(@RequestParam("userId")Integer userId,@RequestParam("hospitalNumber")String hospitalNumber){
        log.info("============user PatientCardController getBlaceByUserId=============param{},{}",userId,hospitalNumber);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("card_no",hospitalNumber);
        PatientCard patientCard = pcService.getOne(queryWrapper);
        BigDecimal balance = patientCard.getBalance();
        return R.ok().data("balance",balance);
    }
    /**
     * todo 根据userId获取余额
     * @param hospitalNumber
     * @return com.buba.utils.R
     * @author xhh
     * @data 2022/6/10 9:10
     */
    @RequestMapping(value = "/getIdByhospitalNumber",method = RequestMethod.POST)
    public R getIdByhospitalNumber(@RequestParam("hospitalNumber")String hospitalNumber){
        log.info("============user PatientCardController getBlaceByUserId=============param{}",hospitalNumber);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("card_no",hospitalNumber);
        PatientCard patientCard = pcService.getOne(queryWrapper);
        return R.ok().data("id",patientCard.getId());
    }
    /**
     * todo 存钱
     *
     * @param money
     * @return com.buba.utils.R
     * @author xhh
     * @data 2022/6/10 14:02
     */
    @Transactional
    @RequestMapping(value = "/saveMoney",method = RequestMethod.POST)
    public R saveMoney(@RequestParam("money")BigDecimal money,@RequestParam("hospitalNumber")String hospitalNumber,@RequestParam("userId")Integer userId,@RequestParam("patientId")Integer patientId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("card_no",hospitalNumber);
        queryWrapper.eq("user_id",userId);
        PatientCard patientCard = pcService.getOne(queryWrapper);
        patientCard.setBalance(patientCard.getBalance().add(money));
        boolean update = pcService.update(patientCard, queryWrapper);
        Record record=new Record();

        record.setRechargeExplain("存钱");
        record.setPatientcardDi(patientCard.getId());
        record.setPatientId(patientId);
        record.setMoney(money);

        if(update) {
            srService.payRecord(record);
            return R.ok();

        }
        else{
            return R.error();
        }

    }
    /**
     * todo 扣钱
     * @param money
     * @param hospitalNumber
     * @return com.buba.utils.R
     * @author xhh
     * @data 2022/6/16 21:04
     */
    @Transactional
    @RequestMapping(value = "/deductMoney",method = RequestMethod.POST)
    public R deductMoney(@RequestParam("money")BigDecimal money,@RequestParam("hospitalNumber")String hospitalNumber) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("card_no",hospitalNumber);
        PatientCard patientCard = pcService.getOne(queryWrapper);
        //金钱不足返回错误
        if(patientCard.getBalance().compareTo(money)==-1) {
            return R.error();
        }
        patientCard.setBalance(patientCard.getBalance().subtract(money));
        boolean update = pcService.update(patientCard, queryWrapper);
        if(update) {
            return R.ok();
        }
        else{
            return R.error();
        }
    }


    /**
     * todo 退钱
     * @param money
     * @param hospitalNumber
     * @return com.buba.utils.R
     * @author xhh
     * @data 2022/6/16 21:04
     */
    @Transactional
    @RequestMapping(value = "/refundMoney",method = RequestMethod.POST)
    public R refundMoney(@RequestParam("money")BigDecimal money,@RequestParam("hospitalNumber")String hospitalNumber,@RequestParam("patientId")Integer patientId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("card_no",hospitalNumber);
        PatientCard patientCard = pcService.getOne(queryWrapper);
        //金钱不足返回错误
        if(patientCard.getBalance().compareTo(money)==-1){
            return R.error();
        }
        patientCard.setBalance(patientCard.getBalance().subtract(money));
        boolean update = pcService.update(patientCard, queryWrapper);

        if(update) {
            Record record=new Record();
            record.setMoney(money);
            record.setPatientId(patientId);
            record.setPatientcardDi(patientCard.getId());
            record.setRechargeExplain("退钱");
            srService.payRecord(record);
            return R.ok();

        } else {
            return R.error();
        }

    }

    /**
     * @param PayVo p
     * @throws
     * @Author 梁玉杰
     * @Description //充值
     * @Return com.buba.utils.R
     * @Date 2022/6/15 16:03
     **/
    @RequestMapping(value = "/pay", method = RequestMethod.POST)
    public R pay(@RequestBody PayVo p) {
        return pcService.pay(p);
    }

    /**
     * @param hospitalNumber hospitalNumber
     * @throws
     * @Author 梁玉杰
     * @Description //根据就诊卡号查询就诊卡还多少钱
     * @Return com.buba.utils.R
     * @Date 2022/6/16 20:40
     **/
    @GetMapping("/getBlaceByhospitalNumber/{hospitalNumber}")
    public R getBlaceByhospitalNumber(@PathVariable String hospitalNumber) {
        return pcService.getBlaceByhospitalNumber(hospitalNumber);
    }


}

